package com.chookss.home.search.fragment;


import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Space;
import android.widget.TextView;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.MyApp;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.home.document.DocumentPreviewActivity;
import com.chookss.home.myTest.MyTestsDetailActivity;
import com.chookss.home.search.adapter.SearchAddressbookAdapter;
import com.chookss.home.search.adapter.SearchDataAdapter;
import com.chookss.home.search.adapter.SearchDiaryAdapter;
import com.chookss.home.search.adapter.SearchGalleryAdapter;
import com.chookss.home.search.adapter.SearchGonggaoAdapter;
import com.chookss.home.search.adapter.SearchInterviewAdapter;
import com.chookss.home.search.adapter.SearchLongVideoAdapter;
import com.chookss.home.search.adapter.SearchNewsAdapter;
import com.chookss.home.search.adapter.SearchPlanAdapter;
import com.chookss.home.search.adapter.SearchSmallVideoAdapter;
import com.chookss.home.search.adapter.SearchTimuAdapter;
import com.chookss.home.search.adapter.SearchTuwenAdapter;
import com.chookss.home.search.entity.SearchAllEntity;
import com.chookss.tiku.AnswerExamActivity;
import com.chookss.tiku.ExamResultActivitys;
import com.chookss.tiku.entity.MyExam;
import com.chookss.tools.AESEncrypt;
import com.chookss.tools.ClearMoreZeroUtil;
import com.chookss.tools.GridSpacingItemDecoration;
import com.chookss.tools.MyEvent;
import com.chookss.tools.MyToast;
import com.chookss.tools.SignUtils2;
import com.chookss.tools.Utils;
import com.chookss.tools.okgoconfig.MyHttpRequest;
import com.chookss.tools.okgoconfig.callback.ObserverCallback;
import com.chookss.tools.okgoconfig.model.LzyResponse;
import com.chookss.view.DrawableTextView;
import com.chookss.view.RecycleViewDivider;
import com.google.gson.reflect.TypeToken;
import com.liulishuo.okdownload.DownloadTask;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.annotations.Nullable;

@SuppressLint("ValidFragment")
public class SearchAllFragment extends SearchBaseFragment {

    public Context mContext;
    Unbinder unbinder;
    @BindView(R.id.rvViewTimu)
    RecyclerView rvViewTimu;
    @BindView(R.id.rvViewSmallVideo)
    RecyclerView rvViewSmallVideo;
    @BindView(R.id.rvViewVideo)
    RecyclerView rvViewLongVideo;
    @BindView(R.id.rvViewPlan)
    RecyclerView rvViewPlan;
    @BindView(R.id.rvViewData)
    RecyclerView rvViewData;
    @BindView(R.id.rvViewLog)
    RecyclerView rvViewDiary;
    @BindView(R.id.rvViewNews)
    RecyclerView rvViewNews;
    @BindView(R.id.rvViewGonggao)
    RecyclerView rvViewGonggao;
    @BindView(R.id.rvViewTest)
    RecyclerView rvViewTest;
    @BindView(R.id.llTestFooter)
    RelativeLayout llTestFooter;
    @BindView(R.id.llTest)
    LinearLayout llTest;
    @BindView(R.id.testTvTitle)
    TextView testTvTitle;
    @BindView(R.id.llDataFooter)
    RelativeLayout llDataFooter;
    @BindView(R.id.llData)
    LinearLayout llData;
    @BindView(R.id.dataTvTitle)
    TextView dataTvTitle;
    @BindView(R.id.rvViewAddressbook)
    RecyclerView rvViewAddressbook;
    @BindView(R.id.rvViewInterview)
    RecyclerView rvViewInterview;
    @BindView(R.id.rvViewTuwen)
    RecyclerView rvViewTuwen;
    @BindView(R.id.rvViewGallery)
    RecyclerView rvViewGallery;
    @BindView(R.id.srlView)
    SmartRefreshLayout srlView;
    @BindView(R.id.tvNone)
    TextView tvNone;
    @BindView(R.id.llNone)
    LinearLayout llNone;
    @BindView(R.id.timuSpace)
    Space timuSpace;
    @BindView(R.id.smallVideoSpace)
    Space smallVideoSpace;
    @BindView(R.id.videoSpace)
    Space videoSpace;
    @BindView(R.id.planSpace)
    Space planSpace;
    @BindView(R.id.dataSpace)
    Space dataSpace;
    @BindView(R.id.logSpace)
    Space diarySpace;
    @BindView(R.id.newsSpace)
    Space newsSpace;
    @BindView(R.id.gonggaoSpace)
    Space gonggaoSpace;
    @BindView(R.id.testSpace)
    Space testSpace;
    @BindView(R.id.addressbookSpace)
    Space addressbookSpace;
    @BindView(R.id.interviewSpace)
    Space interviewSpace;
    @BindView(R.id.tuwenSpace)
    Space tuwenSpace;
    @BindView(R.id.gallerySpace)
    Space gallerySpace;
    //题目
    private SearchTimuAdapter timuAdapter;
    private List<SearchAllEntity.SubjectBean.ListBean> timuList = new ArrayList<>();
    private View timuHeader, timuFooter;
    private TextView timuTvTitle, timuTvInit;
    //小视频
    private SearchSmallVideoAdapter smallVideoAdapter;
    private List<SearchAllEntity.VideoBean.ListBean> smallVideoList = new ArrayList<>();
    private View smallVideoHeader, smallVideoFooter;
    private TextView smallVideoTvTitle, smallVideoTvInit;
    //长视频
    private SearchLongVideoAdapter longVideoAdapter;
    private List<SearchAllEntity.LongVideoBean.ListBean> longVideoList = new ArrayList<>();
    private View longVideoHeader, longVideoFooter;
    private TextView longVideoTvTitle, longVideoTvInit;
    //学习计划
    private SearchPlanAdapter planAdapter;
    private List<SearchAllEntity.PlanBean.ListBean> planList = new ArrayList<>();
    private View planHeader, planFooter;
    private TextView planTvTitle, planTvInit;
    //    //资料库
    private SearchDataAdapter dataAdapter;
    private List<SearchAllEntity.DataBean.ListBean> dataList = new ArrayList<>();
    //    日志
    private SearchDiaryAdapter diaryAdapter;
    private List<SearchAllEntity.DiaryBean.ListBean> diaryList = new ArrayList<>();
    private View diaryHeader, diaryFooter;
    private TextView diaryTvTitle, diaryTvInit;
    //    //新闻
    private SearchNewsAdapter newsAdapter;
    private List<SearchAllEntity.NewsBean.ListBean> newsList = new ArrayList<>();
    private View newsHeader, newsFooter;
    private TextView newsTvTitle, newsTvInit;
    //    //公告
    private SearchGonggaoAdapter gonggaoAdapter;
    private List<SearchAllEntity.NoticeBean.ListBean> gonggaoList = new ArrayList<>();
    private View gonggaoHeader, gonggaoFooter;
    private TextView gonggaoTvTitle, gonggaoTvInit;
    //考试
    private MyTestAdapter testAdapter;
    private List<SearchAllEntity.ExamBean.ListBean> testList = new ArrayList<>();
    private boolean isTest = false;//是否有正在进行的考试
    private int testIndex = -1;//正在进行考试的下标
    //    通讯录
    private SearchAddressbookAdapter addressbookAdapter;
    private List<SearchAllEntity.UserBean.ListBean> addressbookList = new ArrayList<>();
    private View addressbookHeader, addressbookFooter;
    private TextView addressbookTvTitle, addressbookTvInit;
    //面试
    private SearchInterviewAdapter interviewAdapter;
    private List<SearchAllEntity.InterviewBean.ListBean> interviewList = new ArrayList<>();
    private View interviewHeader, interviewFooter;
    private TextView interviewTvTitle, interviewTvInit;
    //图文
    private SearchTuwenAdapter tuwenAdapter;
    private List<SearchAllEntity.ImageTextBean.ListBean> tuwenList = new ArrayList<>();
    private View tuwenHeader, tuwenFooter;
    private TextView tuwenTvTitle, tuwenTvInit;
    //图库
    private SearchGalleryAdapter galleryAdapter;
    private List<SearchAllEntity.ImageBean.ListBean> galleryList = new ArrayList<>();
    private View galleryHeader, galleryFooter;
    private TextView galleryTvTitle, galleryTvInit;

    private Timer mTimer = null;
    private TimerTask mTimerTask = null;
    private String localKey = "", localType = "0";

    public SearchAllFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search_all, container, false);
        unbinder = ButterKnife.bind(this, view);
        init();
        //注册接受
        EventBus.getDefault().register(this);
        return view;
    }

    @Override
    public void onViewStateRestored(@androidx.annotation.Nullable Bundle savedInstanceState) {
        super.onViewStateRestored(savedInstanceState);
    }

    private void init() {
        //题目
        rvViewTimu.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        timuAdapter = new SearchTimuAdapter(R.layout.item_search_timu_list, mContext, timuList);
        rvViewTimu.setAdapter(timuAdapter);
        timuHeader = LayoutInflater.from(mContext).inflate(R.layout.search_header,
                null, false);
        timuFooter = LayoutInflater.from(mContext).inflate(R.layout.search_footer,
                null, false);
        timuTvTitle = timuHeader.findViewById(R.id.tvSearchHeader);
        timuTvInit = timuFooter.findViewById(R.id.tvSearchFooter);
        timuTvInit.setText("更多题目");
        timuAdapter.addHeaderView(timuHeader);
        timuFooter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MyEvent("题目"));
            }
        });
        //小视频
        rvViewSmallVideo.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        smallVideoAdapter = new SearchSmallVideoAdapter(R.layout.item_search_video_list, mContext, smallVideoList);
        rvViewSmallVideo.setAdapter(smallVideoAdapter);
        smallVideoHeader = LayoutInflater.from(mContext).inflate(R.layout.search_header,
                null, false);
        smallVideoFooter = LayoutInflater.from(mContext).inflate(R.layout.search_footer,
                null, false);
        smallVideoTvTitle = smallVideoHeader.findViewById(R.id.tvSearchHeader);
        smallVideoTvInit = smallVideoFooter.findViewById(R.id.tvSearchFooter);
        smallVideoTvInit.setText("更多小视频");
        smallVideoAdapter.addHeaderView(smallVideoHeader);
        smallVideoFooter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MyEvent("小视频"));
            }
        });
        //长视频
        rvViewLongVideo.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        longVideoAdapter = new SearchLongVideoAdapter(R.layout.item_search_video_list, mContext, longVideoList);
        rvViewLongVideo.setAdapter(longVideoAdapter);
        longVideoHeader = LayoutInflater.from(mContext).inflate(R.layout.search_header,
                null, false);
        longVideoFooter = LayoutInflater.from(mContext).inflate(R.layout.search_footer,
                null, false);
        longVideoTvTitle = longVideoHeader.findViewById(R.id.tvSearchHeader);
        longVideoTvInit = longVideoFooter.findViewById(R.id.tvSearchFooter);
        longVideoTvInit.setText("更多视频");
        longVideoAdapter.addHeaderView(longVideoHeader);
        longVideoFooter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MyEvent("视频"));
            }
        });
        //学习计划
        rvViewPlan.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        planAdapter = new SearchPlanAdapter(R.layout.item_search_plan_list, mContext, planList);
        rvViewPlan.setAdapter(planAdapter);
        planHeader = LayoutInflater.from(mContext).inflate(R.layout.search_header,
                null, false);
        planFooter = LayoutInflater.from(mContext).inflate(R.layout.search_footer,
                null, false);
        planTvTitle = planHeader.findViewById(R.id.tvSearchHeader);
        planTvInit = planFooter.findViewById(R.id.tvSearchFooter);
        planTvInit.setText("更多学习计划");
        planAdapter.addHeaderView(planHeader);
        planFooter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MyEvent("学习计划"));
            }
        });
        //资料库
        rvViewData.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        dataAdapter = new SearchDataAdapter(R.layout.item_search_data_list, mContext, dataList, "SearchAllFragment_DownloadFile");
        rvViewData.setAdapter(dataAdapter);
        llDataFooter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MyEvent("资料库"));
            }
        });
        //日志
        rvViewDiary.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        diaryAdapter = new SearchDiaryAdapter(R.layout.item_search_log_list, mContext, diaryList);
        rvViewDiary.setAdapter(diaryAdapter);
        diaryHeader = LayoutInflater.from(mContext).inflate(R.layout.search_header,
                null, false);
        diaryFooter = LayoutInflater.from(mContext).inflate(R.layout.search_footer,
                null, false);
        diaryTvTitle = diaryHeader.findViewById(R.id.tvSearchHeader);
        diaryTvInit = diaryFooter.findViewById(R.id.tvSearchFooter);
        diaryTvInit.setText("更多日志");
        diaryAdapter.addHeaderView(diaryHeader);
        diaryFooter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MyEvent("日志"));
            }
        });
        //新闻
        rvViewNews.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        newsAdapter = new SearchNewsAdapter(R.layout.item_search_news_list, mContext, newsList);
        rvViewNews.setAdapter(newsAdapter);
        newsHeader = LayoutInflater.from(mContext).inflate(R.layout.search_header,
                null, false);
        newsFooter = LayoutInflater.from(mContext).inflate(R.layout.search_footer,
                null, false);
        newsTvTitle = newsHeader.findViewById(R.id.tvSearchHeader);
        newsTvInit = newsFooter.findViewById(R.id.tvSearchFooter);
        newsTvInit.setText("更多新闻");
        newsAdapter.addHeaderView(newsHeader);
        newsFooter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MyEvent("新闻"));
            }
        });
        //公告
        rvViewGonggao.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        gonggaoAdapter = new SearchGonggaoAdapter(R.layout.item_search_gonggao_list, mContext, gonggaoList);
        rvViewGonggao.setAdapter(gonggaoAdapter);
        gonggaoHeader = LayoutInflater.from(mContext).inflate(R.layout.search_header,
                null, false);
        gonggaoFooter = LayoutInflater.from(mContext).inflate(R.layout.search_footer,
                null, false);
        gonggaoTvTitle = gonggaoHeader.findViewById(R.id.tvSearchHeader);
        gonggaoTvInit = gonggaoFooter.findViewById(R.id.tvSearchFooter);
        gonggaoTvInit.setText("更多公告");
        gonggaoAdapter.addHeaderView(gonggaoHeader);
        gonggaoFooter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MyEvent("公告"));
            }
        });
        //考试
        rvViewTest.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        rvViewTest.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.HORIZONTAL, Utils.dip2px(mContext, 0.5f), getResources().getColor(R.color.line_gray)));
        testAdapter = new MyTestAdapter(R.layout.item_search_test_lists, mContext, testList);
        rvViewTest.setAdapter(testAdapter);
        rvViewTest.getItemAnimator().setAddDuration(0);
        rvViewTest.getItemAnimator().setChangeDuration(0);
        rvViewTest.getItemAnimator().setMoveDuration(0);
        rvViewTest.getItemAnimator().setRemoveDuration(0);
        ((SimpleItemAnimator) rvViewTest.getItemAnimator()).setSupportsChangeAnimations(false);
        rvViewTest.setItemAnimator(null);
        llTestFooter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MyEvent("考试"));
            }
        });

        //通讯录
        rvViewAddressbook.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        addressbookAdapter = new SearchAddressbookAdapter(R.layout.item_search_addressbook_list, mContext, addressbookList);
        rvViewAddressbook.setAdapter(addressbookAdapter);
        addressbookHeader = LayoutInflater.from(mContext).inflate(R.layout.search_header,
                null, false);
        addressbookFooter = LayoutInflater.from(mContext).inflate(R.layout.search_footer,
                null, false);
        addressbookTvTitle = addressbookHeader.findViewById(R.id.tvSearchHeader);
        addressbookTvInit = addressbookFooter.findViewById(R.id.tvSearchFooter);
        addressbookTvInit.setText("更多通讯录");
        addressbookAdapter.addHeaderView(addressbookHeader);
        addressbookFooter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MyEvent("通讯录"));
            }
        });

        //面试
        rvViewInterview.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        interviewAdapter = new SearchInterviewAdapter(R.layout.item_search_interview_list, mContext, interviewList);
        rvViewInterview.setAdapter(interviewAdapter);
        interviewHeader = LayoutInflater.from(mContext).inflate(R.layout.search_header,
                null, false);
        interviewFooter = LayoutInflater.from(mContext).inflate(R.layout.search_footer,
                null, false);
        interviewTvTitle = interviewHeader.findViewById(R.id.tvSearchHeader);
        interviewTvInit = interviewFooter.findViewById(R.id.tvSearchFooter);
        interviewTvInit.setText("更多面试");
        interviewAdapter.addHeaderView(interviewHeader);
        interviewFooter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MyEvent("面试"));
            }
        });
        //图文
        rvViewTuwen.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        tuwenAdapter = new SearchTuwenAdapter(R.layout.item_tuwen, mContext, tuwenList);
        rvViewTuwen.setAdapter(tuwenAdapter);
        tuwenHeader = LayoutInflater.from(mContext).inflate(R.layout.search_header,
                null, false);
        tuwenFooter = LayoutInflater.from(mContext).inflate(R.layout.search_footer,
                null, false);
        tuwenTvTitle = tuwenHeader.findViewById(R.id.tvSearchHeader);
        tuwenTvInit = tuwenFooter.findViewById(R.id.tvSearchFooter);
        tuwenTvInit.setText("更多图文");
        tuwenAdapter.addHeaderView(tuwenHeader);
        tuwenFooter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MyEvent("图文"));
            }
        });
        //图库
        GridLayoutManager gridLayoutManager1 = new GridLayoutManager(mContext, 4);
        rvViewGallery.setLayoutManager(gridLayoutManager1);
        GridSpacingItemDecoration gridSpacingItemDecoration = new GridSpacingItemDecoration(4, Utils.dip2px(mContext, 2), false);
        rvViewGallery.addItemDecoration(gridSpacingItemDecoration);
        galleryAdapter = new SearchGalleryAdapter(R.layout.item_gallery, mContext, galleryList,0);
        rvViewGallery.setAdapter(galleryAdapter);
        galleryHeader = LayoutInflater.from(mContext).inflate(R.layout.search_header,
                null, false);
        galleryFooter = LayoutInflater.from(mContext).inflate(R.layout.search_footer,
                null, false);
        galleryTvTitle = galleryHeader.findViewById(R.id.tvSearchHeader);
        galleryTvInit = galleryFooter.findViewById(R.id.tvSearchFooter);
        galleryTvInit.setText("更多图库");
        galleryAdapter.addHeaderView(galleryHeader);
        galleryFooter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new MyEvent("图库"));
            }
        });
        srlView.setEnableLoadMore(false);
        srlView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                getData();
                if (srlView == null) {
                    return;
                }
                srlView.setEnableRefresh(false);
            }
        });
        registerBoradcastReceiver();
    }

    @Override
    public void onVisible() {
        super.onVisible();
        if (srlView == null) {
            return;
        }
        if (Utils.isNull(localKey)) {
            srlView.autoRefresh();
        } else if (testIndex >= 0) {
            srlView.setEnableRefresh(true);
            srlView.autoRefresh();
        } else {
            if (localKey.equals(key) && localType.equals(searchType)) {
            } else {
                srlView.setEnableRefresh(true);
                srlView.autoRefresh();
            }
        }
    }

    @Override
    public void refreshData() {
        srlView.setEnableRefresh(true);
        srlView.autoRefresh();
    }

    private void getData() {
        localKey = key;
        localType = searchType;
        testIndex = -1;
        isTest = false;
        Map<String, String> map = new HashMap<>();
        map.put("word", key);
        map.put("searchType", searchType);
        MyHttpRequest.getDefault().getRequest(new TypeToken<LzyResponse<SearchAllEntity>>() {
        }.getType(), null, Urls.selectForSearch, SignUtils2.makeSignStr(map), false, new ObserverCallback<SearchAllEntity>() {
            @Override
            public void onSuccess(SearchAllEntity data) {
                loadEnd();
                dealData(data);
            }

            @Override
            public void onError(String errorMsg) {
                loadEnd();
                MyToast.show(Utils.onErrorTips(errorMsg));
            }
        });
    }

    private void dealData(SearchAllEntity data) {
        //题目
        if (rvViewTimu == null) {
            return;
        }
        if (data.getSubject() == null) {
            rvViewTimu.setVisibility(View.GONE);
            timuSpace.setVisibility(View.GONE);
        } else {
            timuList.clear();
            if (data.getSubject().getList() != null && data.getSubject().getList().size() > 0) {
                timuList.addAll(data.getSubject().getList());
                timuTvTitle.setText("题目(" + data.getSubject().getTotal() + ")");
                timuAdapter.removeFooterView(timuFooter);
                if (Utils.changeNumber(data.getSubject().getTotal()) > 3) {
                    timuAdapter.addFooterView(timuFooter);
                }
                timuAdapter.setKey(localKey);
                rvViewTimu.setVisibility(View.VISIBLE);
                timuSpace.setVisibility(View.VISIBLE);
                timuAdapter.notifyDataSetChanged();

            } else {
                if (data.getSubject().getList().size() == 0) {
                    rvViewTimu.setVisibility(View.GONE);
                    timuSpace.setVisibility(View.GONE);
                }
            }
        }
        //小视频
        if (data.getVideo() == null) {
            rvViewSmallVideo.setVisibility(View.GONE);
            smallVideoSpace.setVisibility(View.GONE);
        } else {
            smallVideoList.clear();
            if (data.getVideo().getList() != null && data.getVideo().getList().size() > 0) {
                smallVideoList.addAll(data.getVideo().getList());
                smallVideoTvTitle.setText("小视频(" + data.getVideo().getTotal() + ")");
                smallVideoAdapter.removeFooterView(smallVideoFooter);
                if (Utils.changeNumber(data.getVideo().getTotal()) > 3) {
                    smallVideoAdapter.addFooterView(smallVideoFooter);
                }
                smallVideoAdapter.setKey(localKey);
                rvViewSmallVideo.setVisibility(View.VISIBLE);
                smallVideoSpace.setVisibility(View.VISIBLE);
                smallVideoAdapter.notifyDataSetChanged();

            } else {
                if (data.getVideo().getList().size() == 0) {
                    rvViewSmallVideo.setVisibility(View.GONE);
                    smallVideoSpace.setVisibility(View.GONE);
                }
            }
        }
        //长视频
        if (data.getLongVideo() == null) {
            rvViewLongVideo.setVisibility(View.GONE);
            videoSpace.setVisibility(View.GONE);
        } else {
            longVideoList.clear();
            if (data.getLongVideo().getList() != null && data.getLongVideo().getList().size() > 0) {
                longVideoList.addAll(data.getLongVideo().getList());
                longVideoTvTitle.setText("视频(" + data.getLongVideo().getTotal() + ")");
                longVideoAdapter.removeFooterView(longVideoFooter);
                if (Utils.changeNumber(data.getLongVideo().getTotal()) > 3) {
                    longVideoAdapter.addFooterView(longVideoFooter);
                }
                longVideoAdapter.setKey(localKey);
                rvViewLongVideo.setVisibility(View.VISIBLE);
                videoSpace.setVisibility(View.VISIBLE);
                longVideoAdapter.notifyDataSetChanged();
            } else {
                if (data.getLongVideo().getList().size() == 0) {
                    rvViewLongVideo.setVisibility(View.GONE);
                    videoSpace.setVisibility(View.GONE);
                }
            }
        }
        //学习计划
        if (data.getPlan() == null) {
            rvViewPlan.setVisibility(View.GONE);
            planSpace.setVisibility(View.GONE);
        } else {
            planList.clear();
            if (data.getPlan().getList() != null && data.getPlan().getList().size() > 0) {
                planList.addAll(data.getPlan().getList());
                planTvTitle.setText("学习计划(" + data.getPlan().getTotal() + ")");
                planAdapter.removeFooterView(planFooter);
                if (Utils.changeNumber(data.getPlan().getTotal()) > 3) {
                    planAdapter.addFooterView(planFooter);
                }
                planAdapter.setKey(localKey);
                rvViewPlan.setVisibility(View.VISIBLE);
                planSpace.setVisibility(View.VISIBLE);
                planAdapter.notifyDataSetChanged();
            } else {
                if (data.getPlan().getList().size() == 0) {
                    rvViewPlan.setVisibility(View.GONE);
                    planSpace.setVisibility(View.GONE);
                }
            }
        }
        //资料库
        if (data.getData() == null) {
            rvViewData.setVisibility(View.GONE);
            dataSpace.setVisibility(View.GONE);
            llData.setVisibility(View.GONE);
            llDataFooter.setVisibility(View.GONE);
        } else {
            dataList.clear();
            if (data.getData().getList() != null && data.getData().getList().size() > 0) {
                dataList.addAll(data.getData().getList());
                dataTvTitle.setText("资料库(" + data.getData().getTotal() + ")");
                if (Utils.changeNumber(data.getData().getTotal()) > 3) {
                    llDataFooter.setVisibility(View.VISIBLE);
                } else {
                    llDataFooter.setVisibility(View.GONE);
                }
                dataAdapter.setKey(localKey);
                rvViewData.setVisibility(View.VISIBLE);
                dataSpace.setVisibility(View.VISIBLE);
                llData.setVisibility(View.VISIBLE);
                dataAdapter.notifyDataSetChanged();
            } else {
                if (data.getData().getList().size() == 0) {
                    rvViewData.setVisibility(View.GONE);
                    dataSpace.setVisibility(View.GONE);
                    llData.setVisibility(View.GONE);
                    llDataFooter.setVisibility(View.GONE);
                }
            }
        }
        //日志
        if (data.getDiary() == null) {
            rvViewDiary.setVisibility(View.GONE);
            diarySpace.setVisibility(View.GONE);
        } else {
            diaryList.clear();
            if (data.getDiary().getList() != null && data.getDiary().getList().size() > 0) {
                diaryList.addAll(data.getDiary().getList());
                diaryTvTitle.setText("日志(" + data.getDiary().getTotal() + ")");
                diaryAdapter.removeFooterView(diaryFooter);
                if (Utils.changeNumber(data.getDiary().getTotal()) > 3) {
                    diaryAdapter.addFooterView(diaryFooter);
                }
                diaryAdapter.setKey(localKey);
                rvViewDiary.setVisibility(View.VISIBLE);
                diarySpace.setVisibility(View.VISIBLE);
                diaryAdapter.notifyDataSetChanged();
            } else {
                if (data.getDiary().getList().size() == 0) {
                    rvViewDiary.setVisibility(View.GONE);
                    diarySpace.setVisibility(View.GONE);
                }
            }
        }
        //新闻
        if (data.getNews() == null) {
            rvViewNews.setVisibility(View.GONE);
            newsSpace.setVisibility(View.GONE);
        } else {
            newsList.clear();
            if (data.getNews().getList() != null && data.getNews().getList().size() > 0) {
                newsList.addAll(data.getNews().getList());
                newsTvTitle.setText("新闻(" + data.getNews().getTotal() + ")");
                newsAdapter.removeFooterView(newsFooter);
                if (Utils.changeNumber(data.getNews().getTotal()) > 3) {
                    newsAdapter.addFooterView(newsFooter);
                }
                newsAdapter.setKey(localKey);
                rvViewNews.setVisibility(View.VISIBLE);
                newsSpace.setVisibility(View.VISIBLE);
                newsAdapter.notifyDataSetChanged();
            } else {
                if (data.getNews().getList().size() == 0) {
                    rvViewNews.setVisibility(View.GONE);
                    newsSpace.setVisibility(View.GONE);
                }
            }
        }
        //公告
        if (data.getNotice() == null) {
            rvViewGonggao.setVisibility(View.GONE);
            gonggaoSpace.setVisibility(View.GONE);
        } else {
            gonggaoList.clear();
            if (data.getNotice().getList() != null && data.getNotice().getList().size() > 0) {
                gonggaoList.addAll(data.getNotice().getList());
                gonggaoTvTitle.setText("公告(" + data.getNotice().getTotal() + ")");
                gonggaoAdapter.removeFooterView(gonggaoFooter);
                if (Utils.changeNumber(data.getNotice().getTotal()) > 3) {
                    gonggaoAdapter.addFooterView(gonggaoFooter);
                }
                gonggaoAdapter.setKey(localKey);
                rvViewGonggao.setVisibility(View.VISIBLE);
                gonggaoSpace.setVisibility(View.VISIBLE);
                gonggaoAdapter.notifyDataSetChanged();
            } else {
                if (data.getNotice().getList().size() == 0) {
                    rvViewGonggao.setVisibility(View.GONE);
                    gonggaoSpace.setVisibility(View.GONE);
                }
            }
        }
        //考试
        if (data.getExam() == null) {
            rvViewTest.setVisibility(View.GONE);
            testSpace.setVisibility(View.GONE);
            llTest.setVisibility(View.GONE);
            llTestFooter.setVisibility(View.GONE);
        } else {
            testList.clear();
            if (data.getExam().getList() != null && data.getExam().getList().size() > 0) {
                for (int i = 0; i < data.getExam().getList().size(); i++) {
                    if ("3".equals(data.getExam().getList().get(i).getExamStatus())) {
                        isTest = true;
                        allTime = Utils.changeNumber(data.getExam().getList().get(i).getSeconds());
                        testIndex = i;
                        startTimer();
                    }
                }
                testTvTitle.setText("考试(" + data.getExam().getTotal() + ")");
                if (Utils.changeNumber(data.getExam().getTotal()) > 3) {
                    llTestFooter.setVisibility(View.VISIBLE);
                } else {
                    llTestFooter.setVisibility(View.GONE);
                }
                testList.addAll(data.getExam().getList());
                rvViewTest.setVisibility(View.VISIBLE);
                testSpace.setVisibility(View.VISIBLE);
                llTest.setVisibility(View.VISIBLE);
                testAdapter.notifyDataSetChanged();
            } else {
                if (data.getExam().getList().size() == 0) {
                    rvViewTest.setVisibility(View.GONE);
                    testSpace.setVisibility(View.GONE);
                    llTest.setVisibility(View.GONE);
                    llTestFooter.setVisibility(View.GONE);
                }
            }
        }
        //通讯录
        if (data.getUser() == null) {
            rvViewAddressbook.setVisibility(View.GONE);
            addressbookSpace.setVisibility(View.GONE);
        } else {
            addressbookList.clear();
            if (data.getUser().getList() != null && data.getUser().getList().size() > 0) {
                addressbookList.addAll(data.getUser().getList());
                addressbookTvTitle.setText("通讯录(" + data.getUser().getTotal() + ")");
                addressbookAdapter.removeFooterView(addressbookFooter);
                if (Utils.changeNumber(data.getUser().getTotal()) > 3) {
                    addressbookAdapter.addFooterView(addressbookFooter);
                }
                addressbookAdapter.setKey(localKey);
                rvViewAddressbook.setVisibility(View.VISIBLE);
                addressbookSpace.setVisibility(View.VISIBLE);
                addressbookAdapter.notifyDataSetChanged();
            } else {
                if (data.getUser().getList().size() == 0) {
                    rvViewAddressbook.setVisibility(View.GONE);
                    addressbookSpace.setVisibility(View.GONE);
                }
            }
        }
        //面试
        if (data.getInterview() == null) {
            rvViewInterview.setVisibility(View.GONE);
            interviewSpace.setVisibility(View.GONE);
        } else {
            interviewList.clear();
            if (data.getInterview().getList() != null && data.getInterview().getList().size() > 0) {
                interviewList.addAll(data.getInterview().getList());
                interviewTvTitle.setText("面试(" + data.getInterview().getTotal() + ")");
                interviewAdapter.removeFooterView(interviewFooter);
                if (Utils.changeNumber(data.getInterview().getTotal()) > 3) {
                    interviewAdapter.addFooterView(interviewFooter);
                }
                interviewAdapter.setKey(localKey);
                rvViewInterview.setVisibility(View.VISIBLE);
                interviewSpace.setVisibility(View.VISIBLE);
                interviewAdapter.notifyDataSetChanged();
            } else {
                if (data.getInterview().getList().size() == 0) {
                    rvViewInterview.setVisibility(View.GONE);
                    interviewSpace.setVisibility(View.GONE);
                }
            }
        }
        //图文
        if (data.getImageText() == null) {
            rvViewTuwen.setVisibility(View.GONE);
            tuwenSpace.setVisibility(View.GONE);
        } else {
            tuwenList.clear();
            if (data.getImageText().getList() != null && data.getImageText().getList().size() > 0) {
                tuwenList.addAll(data.getImageText().getList());
                tuwenTvTitle.setText("图文(" + data.getImageText().getTotal() + ")");
                tuwenAdapter.removeFooterView(tuwenFooter);
                if (Utils.changeNumber(data.getImageText().getTotal()) > 3) {
                    tuwenAdapter.addFooterView(tuwenFooter);
                }
                tuwenAdapter.setKey(localKey);
                rvViewTuwen.setVisibility(View.VISIBLE);
                tuwenSpace.setVisibility(View.VISIBLE);
                tuwenAdapter.notifyDataSetChanged();
            } else {
                if (data.getImageText().getList().size() == 0) {
                    rvViewTuwen.setVisibility(View.GONE);
                    tuwenSpace.setVisibility(View.GONE);
                }
            }
        }
        //图库
        if (data.getImage() == null) {
            rvViewGallery.setVisibility(View.GONE);
            gallerySpace.setVisibility(View.GONE);
        } else {
            galleryList.clear();
            if (data.getImage().getList() != null && data.getImage().getList().size() > 0) {
                if(data.getImage().getList().size()<=4){
                    galleryList.addAll(data.getImage().getList());
                }else if(data.getImage().getList().size()>4&&data.getImage().getList().size()<8){
                    galleryList.addAll(data.getImage().getList().subList(0,4));
                }else{
                    galleryList.addAll(data.getImage().getList().subList(0,8));
                }
                galleryTvTitle.setText("图库(" + data.getImage().getTotal() + ")");
                galleryAdapter.removeFooterView(galleryFooter);
                if (Utils.changeNumber(data.getImage().getTotal()) > 8) {
                    galleryAdapter.addFooterView(galleryFooter);
                }
                galleryAdapter.setKey(localKey);
                rvViewGallery.setVisibility(View.VISIBLE);
                gallerySpace.setVisibility(View.VISIBLE);
                galleryAdapter.notifyDataSetChanged();
            } else {
                if (data.getImage().getList().size() == 0) {
                    rvViewGallery.setVisibility(View.GONE);
                    gallerySpace.setVisibility(View.GONE);
                }
            }
        }
        if (data == null) {
            String init = "抱歉，没用找到关于“<font color='#666666'>" + key + "</font>”的信息";
            tvNone.setText(Html.fromHtml(init));
            if (srlView == null || llNone == null) {
                return;
            }
            llNone.setVisibility(View.VISIBLE);
            srlView.setVisibility(View.GONE);
        } else if (data.getSubject() == null & data.getVideo() == null & data.getLongVideo() == null & data.getPlan() == null & data.getData() == null & data.getDiary() == null & data.getNotice() == null & data.getNews() == null & data.getExam() == null & data.getUser() == null && data.getInterview() == null && data.getImageText() == null && data.getImage() == null) {
            String init = "抱歉，没用找到关于“<font color='#666666'>" + key + "</font>”的信息";
            tvNone.setText(Html.fromHtml(init));
            if (srlView == null || llNone == null) {
                return;
            }
            llNone.setVisibility(View.VISIBLE);
            srlView.setVisibility(View.GONE);
        } else {
            if (srlView == null || llNone == null) {
                return;
            }
            llNone.setVisibility(View.GONE);
            srlView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void initImmersionBar() {

    }

    class MyTestAdapter extends BaseQuickAdapter<SearchAllEntity.ExamBean.ListBean, BaseViewHolder> {
        private Context context;
        private int type;

        public MyTestAdapter(int layoutResId, Context context, @Nullable List<SearchAllEntity.ExamBean.ListBean> data) {
            super(layoutResId, data);
            this.context = context;
        }

        @Override
        protected void convert(BaseViewHolder helper, SearchAllEntity.ExamBean.ListBean item) {
            TextView tvTitle = helper.getView(R.id.tvTitle);
            TextView tvSeconds = helper.getView(R.id.tvSeconds);
            TextView tvScore = helper.getView(R.id.tvScore);
            DrawableTextView tvTime = helper.getView(R.id.tvTime);
            TextView tvTimeLength = helper.getView(R.id.tvTimeLength);
            TextView tvInit = helper.getView(R.id.tvInit);
            TextView tvPublishTime = helper.getView(R.id.tvPublishTime);
            TextView tvContinue = helper.getView(R.id.tvContinue);
            TextView tvStatus = helper.getView(R.id.tvStatus);
            TextView tvExamineeName = helper.getView(R.id.tvExamineeName);
            TextView tvExamPublishName = helper.getView(R.id.tvExamPublishName);
            //考试状态  0 缺考 1 未完成(101 考试尚未开始 102 考试已经开始) 2 已完成 3进行中

            if ("3".equals(item.getExamStatus())) {
                tvContinue.setVisibility(View.VISIBLE);
                tvStatus.setVisibility(View.GONE);
                tvSeconds.setVisibility(View.VISIBLE);
                tvScore.setVisibility(View.GONE);
                tvSeconds.setTextColor(Color.parseColor("#E12416"));
                tvSeconds.setBackgroundResource(R.color.white);
                tvSeconds.setText("剩余" + Utils.changeSeconds(Utils.changeNumber(item.getSeconds())));
            } else if ("0".equals(item.getExamStatus())) {
                tvContinue.setVisibility(View.GONE);
                tvStatus.setVisibility(View.VISIBLE);
                tvSeconds.setVisibility(View.VISIBLE);
                tvScore.setVisibility(View.GONE);
                tvSeconds.setTextColor(Color.parseColor("#999999"));
                tvSeconds.setBackgroundResource(R.color.white);
                tvStatus.setText("查看试卷");
                tvSeconds.setText("缺考");
            } else if ("101".equals(item.getExamStatus())) {
                tvContinue.setVisibility(View.GONE);
                tvStatus.setVisibility(View.VISIBLE);
                tvSeconds.setVisibility(View.VISIBLE);
                tvScore.setVisibility(View.GONE);
                tvSeconds.setTextColor(Color.parseColor("#FCA817"));
                tvSeconds.setBackgroundResource(R.color.white);
                tvStatus.setText("等待考试");
                tvSeconds.setText(Utils.getExamTime(item.getSeconds()) + "后开考");
            } else if ("102".equals(item.getExamStatus())) {
                tvContinue.setVisibility(View.GONE);
                tvStatus.setVisibility(View.VISIBLE);
                tvSeconds.setVisibility(View.VISIBLE);
                tvScore.setVisibility(View.GONE);
                tvSeconds.setTextColor(Color.parseColor("#00C993"));
                tvSeconds.setBackgroundResource(R.color.white);
                tvStatus.setText("进入考场");
                tvSeconds.setText(Utils.getExamTime(item.getSeconds()) + "后结束");
            } else if ("2".equals(item.getExamStatus())) {
                tvContinue.setVisibility(View.GONE);
                tvStatus.setVisibility(View.VISIBLE);
                tvSeconds.setVisibility(View.GONE);
                tvScore.setVisibility(View.VISIBLE);
                tvStatus.setText("查看试卷");
                tvScore.setText(ClearMoreZeroUtil.subZeroAndDot(item.getScore()) + "分");
            }
            tvTitle.setText(Utils.matcherSearchTitle(context.getResources().getColor(R.color.main_color), item.getExamName(), localKey));
            tvTime.setText("有效期：" + Utils.clearZeroTimes(item.getStartTime()) + "-" + Utils.clearZeroTimes(item.getEndTime()));
            tvTimeLength.setText("考试时长：" + item.getExamTimeLength() + "分钟");

            if (Utils.isNull(item.getExamDetail())) {
                tvInit.setVisibility(View.GONE);
            } else {
                tvInit.setText("备注：" + item.getExamDetail());
                tvInit.setVisibility(View.VISIBLE);
            }
            tvPublishTime.setText("发布时间：" + item.getCreateTime());
            if (Utils.isNull(item.getEmployeeNames())) {
                tvExamineeName.setVisibility(View.GONE);
            } else {
                tvExamineeName.setVisibility(View.VISIBLE);
                tvExamineeName.setText("考生：");
                tvExamineeName.append(Utils.matcherSearchTitle(context.getResources().getColor(R.color.main_color), item.getEmployeeNames(), key));
            }
            if (Utils.isNull(item.getCreateEmployeeName())) {
                tvExamPublishName.setVisibility(View.GONE);
            } else {
                tvExamPublishName.setVisibility(View.VISIBLE);
                tvExamPublishName.setText("创建人：");
                tvExamPublishName.append(Utils.matcherSearchTitle(context.getResources().getColor(R.color.main_color), item.getCreateEmployeeName(), key));
            }
            tvContinue.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Urls.antiShake.check()) {
                        return;
                    }
                    MyExam myExam = Utils.modelA2B(item);
                    Intent intent = new Intent(mContext, AnswerExamActivity.class);
                    intent.putExtra("answerType", "1");
                    intent.putExtra("exam", myExam);
                    startActivity(intent);
                }
            });
            tvStatus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Urls.antiShake.check()) {
                        return;
                    }
                    //考试状态  0 缺考 1 未完成(101 考试尚未开始 102 考试已经开始) 2 已完成 3进行中
                    MyExam myExam = Utils.modelA2B(item);
                    if ("101".equals(item.getExamStatus())) {
                        Intent intent = new Intent(mContext, MyTestsDetailActivity.class);
                        intent.putExtra("examCode", item.getExamCode());
                        intent.putExtra("recordType", "4");
                        intent.putExtra("examStatus", item.getExamStatus());
                        intent.putExtra("exam", myExam);
                        startActivity(intent);
                    } else if ("102".equals(item.getExamStatus())) {
                        if (isTest) {
                            MyToast.show("请先完成正在进行的考试");
                            return;
                        }
                        Intent intent = new Intent(mContext, MyTestsDetailActivity.class);
                        intent.putExtra("examCode", item.getExamCode());
                        intent.putExtra("examStatus", item.getExamStatus());
                        intent.putExtra("recordType", "4");
                        intent.putExtra("exam", myExam);
                        startActivity(intent);
                    } else if ("3".equals(item.getExamStatus())) {
                        Intent intent = new Intent(mContext, AnswerExamActivity.class);
                        intent.putExtra("answerType", "1");
                        intent.putExtra("exam", myExam);
                        startActivity(intent);
                    } else if ("2".equals(item.getExamStatus())) {
                        Intent intent = new Intent(mContext, ExamResultActivitys.class);
                        intent.putExtra("recordType", "4");
                        intent.putExtra("pmKey", item.getRecordUid());
                        intent.putExtra("examCode", item.getExamCode());
                        intent.putExtra("examName", item.getExamName());
                        startActivity(intent);
                    } else if ("0".equals(item.getExamStatus())) {
                        Intent intent = new Intent(mContext, ExamResultActivitys.class);
                        intent.putExtra("recordType", "4");
                        intent.putExtra("pmKey", item.getExamCode());
                        intent.putExtra("isMiss", 1);
                        intent.putExtra("examCode", item.getExamCode());
                        intent.putExtra("examName", item.getExamName());
                        startActivity(intent);
                    }
                }
            });
        }
    }

    private void loadEnd() {
        if (null != srlView && srlView.getState() == RefreshState.Refreshing)
            srlView.finishRefresh(true);
        if (null != srlView && srlView.getState() == RefreshState.Loading)
            srlView.finishLoadMore(true);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(MyEvent messageEvent) {
        //事件订阅者处理事件
        if (messageEvent.getHashMap() != null) {
            HashMap<String, String> map = messageEvent.getHashMap();
            if (map.containsKey("subjectCode") && map.containsKey("collectFlag")) {
                refreshSubject(map.get("subjectCode"), map.get("collectFlag"));
            }else   if(map.containsKey("DocumentPreview_error")){
                for (int i = 0; i < dataList.size(); i++) {
                    if (dataList.get(i).getDataCode().equals(map.get("DocumentPreview_error"))) {
                        dataList.get(i).setError(true);
                    }
                }
            }
        }
    }

    private void refreshSubject(String subjectCode, String collectFlag) {
        for (int i = 0; i < timuList.size(); i++) {
            SearchAllEntity.SubjectBean.ListBean bean = timuList.get(i);
            if (bean.getSubjectCode().equals(subjectCode)) {
                bean.setCollectFlag(collectFlag);
                timuAdapter.notifyItemChanged(i + 1);
            }
        }
    }

    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                int position = intent.getIntExtra("position", 0);
                int percent = intent.getIntExtra("percent", 0);
                String recevierDataCode = intent.getStringExtra("dataCode");
                boolean encryptStatus = intent.getBooleanExtra("encryptStatus", false);
                String fileUrl = intent.getStringExtra("fileUrl");
                String fileName = intent.getStringExtra("fileName");
                for (int i = 0; i < dataList.size(); i++) {
                    if (dataList.get(i).getDataCode().equals(recevierDataCode)) {
                        if (!dataList.get(i).isOpen()) {
                            if (encryptStatus) {
                                dataList.get(i).setOpen(true);
                            }
                            dealData(position, percent, recevierDataCode, encryptStatus, fileUrl, fileName);
                        }

                    }
                }

            }
        }

    };

    /**
     * @param position
     * @param percent
     * @param recevierDataCode 处理广播数据
     * @param encryptStatus
     * @param fileUrl
     * @param fileName
     */
    private void dealData(int position, int percent, String recevierDataCode, boolean encryptStatus, String fileUrl, String fileName) {

        if (encryptStatus) {
            if (!MyApp.docunmentOpen) {
                Intent newIntent = new Intent(mContext, DocumentPreviewActivity.class);
                newIntent.putExtra("fileUrl", fileUrl);
                newIntent.putExtra("fileName", fileName);
                newIntent.putExtra("dataCode", recevierDataCode);
                startActivity(newIntent);
            }
        } else {
            if (percent > 0 && percent < 100) {
                if (dataAdapter != null) {
                    dataList.get(position).setProgress(percent);
                    dataAdapter.notifyItemChanged(position);
                }
            } else if (percent == 100) {
                if (dataAdapter != null) {
                    dataList.get(position).setProgress(100);
                    dataAdapter.notifyItemChanged(position);
                    new Thread() {
                        @Override
                        public void run() {
                            AESEncrypt.encryptFile(mContext, fileUrl, fileName, recevierDataCode, "SearchAllFragment_DownloadFile");
                        }
                    }.start();
                }


            } else if (percent == 0) {
                //正常情况不会0
                Intent newIntent = new Intent(mContext, DocumentPreviewActivity.class);
                newIntent.putExtra("fileUrl", fileUrl);
                newIntent.putExtra("fileName", fileName);
                newIntent.putExtra("dataCode", recevierDataCode);
                newIntent.putExtra("isError", true);
                startActivity(newIntent);
            }
        }
    }

    private void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction("SearchAllFragment_DownloadFile");// 获取数据
        LocalBroadcastManager.getInstance(mContext).registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        HashMap<String, DownloadTask> map = MyApp.downloadTaskHashMap;
        for (String key : map.keySet()) {
            DownloadTask task = map.get(key);
            if (task != null) {
                task.cancel();
            }
        }
        if (mBroadcastReceiver != null && mContext != null) {
            LocalBroadcastManager.getInstance(mContext).unregisterReceiver(mBroadcastReceiver);
        }
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
        stopTimer();
    }

    private void startTimer() {
        if (mTimer == null) {
            mTimer = new Timer();
        }
        if (mTimerTask == null) {
            mTimerTask = new TimerTask() {
                @Override
                public void run() {
                    //每分中上传数据
                    Message message = new Message();
                    message.what = 1001;
                    handler.sendMessage(message);
                }
            };
            if (mTimer != null && mTimerTask != null) {
                mTimer.schedule(mTimerTask, 1000, 1000);//执行定时器中的任务
            }
        }

    }

    private int allTime = 0;
    // handler类接收数据
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1001) {
                allTime--;
                if (isTest) {
                    if (allTime >= 0) {
                        if (testList != null && testList.size() > testIndex) {
                            testList.get(testIndex).setSeconds(allTime + "");
                            testAdapter.notifyItemChanged(testIndex);
                        }
                    }
                }
            }
        }

    };

    /**
     * 停止定时器，初始化定时器开关
     */
    private void stopTimer() {
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
        if (mTimerTask != null) {
            mTimerTask.cancel();
            mTimerTask = null;
        }
    }
}
